package com.onpositive.notes.parser;

import java.io.Serializable;
import java.util.ArrayList;

public class SentenceSplitter implements Serializable {
	
	private static final long serialVersionUID = 595686775044877400L;

	public SentenceSplitter() {
		super();
	}

	public static final String META_KEY_MARK_TAG = "mark-tag";
	public static final String MARK_TAG_COMA = "mark-coma";
	public static final String MARK_TAG_LINE_BREAK = "mark-line-break";

	public static ArrayList<Block<Mark>> extractMarks( String str ){
		
		String[] separators = new String[]{ System.getProperty("line.separator"), "\r\n", "\n" } ;
		String[] coma = new String[]{ "," } ;
		
		ArrayList<Block<Mark>> split = split(str, separators );
		for( Block<?> blk : split )
			blk.putMetaInfo(META_KEY_MARK_TAG, MARK_TAG_LINE_BREAK ) ;

		ArrayList<Block<Mark>> split0 = split(str, coma);
//		split0.remove(split0.size()-1) ;
		for( Block<?> blk : split0 )
			blk.putMetaInfo(META_KEY_MARK_TAG, MARK_TAG_COMA ) ;

		split.addAll(split0) ;
		return split;
	}

	public static ArrayList<Block<Mark>> split(String str,	String[] separators ){
		
		if( str == null )
			return null ;
		
		ArrayList<Block<Mark>> result = new ArrayList<Block<Mark>>() ;
		
		
		int length = str.length() ;
l0:		for( int i = 0 ; i < length ; ){
			
			for( String s : separators ){
				if( str.startsWith(s, i) ){
					Mark del = new Mark(s) ;
					int lng = s.length();
					Block<Mark> blk = new Block<Mark>(str, i, i+lng, del, Block.BLOCK_KIND_MARK) ;
					result.add(blk) ;
					i+=lng;
					continue l0;
				}
			}
			i++ ;
		}
		//result.add( new Block<Mark>(str, length, length, new Mark(""), Block.BLOCK_KIND_MARK) ) ;
		return result ;
	}

}
